//---------------------------------------------------------------------
//  This file is part of the Background Motion solution.
// 
//  Copyright (C) Mindscape (TM).  All rights reserved.
//  http://www.mindscape.co.nz
// 
//  THIS CODE AND INFORMATION ARE PROVIDED AS IS WITHOUT WARRANTY OF ANY
//  KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
//  IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
//  PARTICULAR PURPOSE.
//---------------------------------------------------------------------

using Mindscape.BackgroundMotion.Model;
using Mindscape.BackgroundMotion.Website.Controllers;
using Mindscape.BackgroundMotion.Website.Infrastructure;

namespace Mindscape.BackgroundMotion.Website.Views
{
  public sealed class AddPresenter : PresenterBase<IAddView, IContributionController>
  {
    /// <summary>
    /// This method is invoked by the view everytime it loads
    /// </summary>
    public override void OnViewLoaded()
    {
      View.Contribution = new Contribution();
      View.Contribution.Contributor = Controller.CurrentMember;

      View.ContentTypes = ReferenceDataProvider.ContentTypes;
    }

    /// <summary>
    /// Saves the current view <see ref="Contribution"/> instance.
    /// </summary>
    public void Save()
    {
      if (View.Contribution.ValidationResults.IsValid)
      {
        Controller.AddContribution(View.Contribution);
      }
      else
      {
        View.ValidationResults = View.Contribution.ValidationResults;
      }
    }

    /// <summary>
    /// Loads the contribution from an external provider.
    /// </summary>
    /// <param name="provider">The name of the external provider.</param>
    /// <param name="identifier">The identifier which will be passed to the provider.</param>
    public void LoadContributionFromProvider(string provider, string identifier)
    {
      Contribution newContrib = Controller.GetContributionFromExternalProvider(provider, identifier);

      if (View.Contribution.DownloadLink.Length == 0)
      {
        View.Contribution.DownloadLink = newContrib.DownloadLink;
      }
      if (View.Contribution.PreviewLink.Length == 0)
      {
        View.Contribution.PreviewLink = newContrib.PreviewLink;
      }
      if (View.Contribution.PreviewImage.Length == 0)
      {
        View.Contribution.PreviewImage = newContrib.PreviewImage;
      }
      if (View.Contribution.GeoXString.Length == 0)
      {
        View.Contribution.GeoX = newContrib.GeoX;
      }
      if (View.Contribution.GeoYString.Length == 0)
      {
        View.Contribution.GeoY = newContrib.GeoY;
      }
      if (View.Contribution.Description.Length == 0)
      {
        View.Contribution.Description = newContrib.Description;
      }
      if (View.Contribution.Title.Length == 0)
      {
        View.Contribution.Title = newContrib.Title;
      }
      if (View.Contribution.Tags.Length == 0)
      {
        View.Contribution.Tags = newContrib.Tags;
      }

      // View.Contribution = 
      View.ShowForm();
    }
  }
}